package Client.ui;


import javax.swing.JFrame;
import java.awt.GridLayout;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JButton;


import Client.Controler.AppliManager;
import VWorld.Avatar;
import VWorld.Height;
import VWorld.HeightImpl;
import VWorld.Humor;
import VWorld.HumorImpl;
import VWorld.Sex;
import VWorld.SexImpl;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CreateAvatarWindow {

	private JDialog frame;
	private JTextField tflogin;
	private JPasswordField tfpassword;
	private JTextField tfemail;
	private HashMap<Humor, String> humors;
	private HashMap<Sex, String> sexs;
	private HashMap<Height, String> heights;
	private JFrame mainWindow;
	private AppliManager appliManager;
	private JComboBox cbhumors;
	private JComboBox cbheights;
	private JComboBox cbsexs;
	private Avatar toSee;
	private boolean chatWith;
	private boolean logout;

	/**
	 * Create the application.
	 * @param avatarManagerRef 
	 */
	public CreateAvatarWindow(AppliManager appliManager) {
		this(appliManager, null);
	}

	public CreateAvatarWindow(AppliManager appliManager2, Avatar avatar) {
		this(appliManager2, avatar, true);
	}

	public CreateAvatarWindow(AppliManager appliManager2, Avatar avatar,
			boolean pchatWith) {
		this(appliManager2, avatar, pchatWith, false);
	}
	
	public CreateAvatarWindow(AppliManager appliManager2, Avatar avatar,
			boolean pchatWith, boolean plogout) {
		this.appliManager = appliManager2;
		this.toSee = avatar;
		this.chatWith = pchatWith;
		this.logout = plogout;
		this.initialize();
		this.frame.setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JDialog(this.mainWindow);
		frame.setBounds(100, 100, 338, 300);
		//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new GridLayout(8, 2, 0, 0));
		frame.setTitle("Create avatar");
		
		JLabel lblNewLabel = new JLabel("Create your avatar");
		frame.getContentPane().add(lblNewLabel);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel);
		
		JLabel lblNewLabel_1 = new JLabel("Login:");
		frame.getContentPane().add(lblNewLabel_1);
		
		tflogin = this.getTextFieldLogin();
		frame.getContentPane().add(tflogin);
		tflogin.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password:");
		frame.getContentPane().add(lblPassword);
		
		tfpassword = this.getTFPassword();
		frame.getContentPane().add(tfpassword);
		tfpassword.setColumns(10);
		
		JLabel lblEmail = new JLabel("Email:");
		frame.getContentPane().add(lblEmail);
		
		tfemail = this.getTFEmail();
		frame.getContentPane().add(tfemail);
		tfemail.setColumns(10);
		
		JLabel lblHumor = new JLabel("Humor:");
		frame.getContentPane().add(lblHumor);
		
		this.cbhumors = this.getCBHumors();
		frame.getContentPane().add(cbhumors);
		
		JLabel lblHeight = new JLabel("Height:");
		frame.getContentPane().add(lblHeight);
		
		this.cbheights = this.getCBHeights();
		frame.getContentPane().add(cbheights);
		
		JLabel lblSex = new JLabel("Sex:");
		frame.getContentPane().add(lblSex);
		
		this.cbsexs = this.getCBSexs();
		frame.getContentPane().add(cbsexs);
		
		JPanel panel_1 = new JPanel();
		frame.getContentPane().add(panel_1);
		
		JButton btnOk = this.getBtnOk();
		frame.getContentPane().add(btnOk);
		
		
	}

	private JTextField getTFEmail() {
		JTextField tfemail = new JTextField();
		if(this.toSee != null){
			tfemail.setText(this.toSee.email);
			tfemail.setEditable(false);
		}else if(this.appliManager.userConnected()){
			tfemail.setText(this.appliManager.getEmail());
		}
		return tfemail;
	}

	private JComboBox getCBHeights() {
		JComboBox cbheights;
		
		this.heights = HeightImpl.getHeights();

		if(this.toSee != null){
			String [] tab = new String[1];
			tab[0] = this.heights.get(this.toSee.height);
			cbheights = new JComboBox(tab);
			cbheights.setEditable(false);
		}else if(this.appliManager.userConnected()){
			String [] tab = new String[this.heights.size()];
			int cpt = 0, index = 0;
			Iterator<Height> ite1 = this.heights.keySet().iterator();
			Height current;
			while(ite1.hasNext()){
				current = ite1.next();
				if(this.appliManager.getHeight().value() == current.value()){
					index = cpt;
				}
				tab[cpt++] = this.heights.get(current);
			}
			
			cbheights = new JComboBox(tab);
			cbheights.setSelectedIndex(index);
		}else{
			String [] tab = new String[this.heights.size()];
			int cpt = 0;
			Iterator<Height> ite2 = this.heights.keySet().iterator();
			while(ite2.hasNext()){
				tab[cpt++] = this.heights.get(ite2.next());
			}
			
			cbheights = new JComboBox(tab);
		}
		
		
		return cbheights;
	}

	private JComboBox getCBSexs() {
		JComboBox cbsexs;
		
		this.sexs = SexImpl.getSexs();

		if(this.toSee != null){
			String [] tab = new String[1];
			tab[0] = this.sexs.get(this.toSee.sex);
			cbsexs = new JComboBox(tab);
			cbsexs.setEditable(false);
		}else if (this.appliManager.userConnected()){
			String [] tab = new String[this.sexs.size()];
			int cpt = 0, index = 0;
			Iterator<Sex> ite1 = this.sexs.keySet().iterator();
			Sex current;
			while(ite1.hasNext()){
				current = ite1.next();
				if(this.appliManager.getSex().value() == current.value()){
					index = cpt;
				}
				tab[cpt++] = this.sexs.get(current);
			}
			
			cbsexs = new JComboBox(tab);
			cbsexs.setSelectedIndex(index);
		}else{
			String [] tab = new String[this.sexs.size()];
			int cpt = 0;
			Iterator<Sex> ite3 = this.sexs.keySet().iterator();
			while(ite3.hasNext()){
				tab[cpt++] = this.sexs.get(ite3.next());
			}
			
			cbsexs = new JComboBox(tab);
		}
		
		return cbsexs;
	}

	private JComboBox getCBHumors() {
		JComboBox cbHumor;
		
		this.humors = HumorImpl.getHumors();
		
		if(this.toSee != null){
			String [] tab = new String[1];
			tab[0] = this.humors.get(this.toSee.humor);
			cbHumor = new JComboBox(tab);
			cbHumor.setEditable(false);
		}else if(this.appliManager.userConnected()){
			String [] tab = new String[this.humors.size()];
			int cpt = 0, index = 0;
			Iterator<Humor> ite1 = this.humors.keySet().iterator();
			Humor current;
			while(ite1.hasNext()){
				current = ite1.next();
				if(this.appliManager.getHumor().value() == current.value()){
					index = cpt;
				}
				tab[cpt++] = this.humors.get(current);
			}
			
			cbHumor = new JComboBox(tab);
			cbHumor.setSelectedIndex(index);
		}else{
			String [] tab = new String[this.humors.size()];
			int cpt = 0;
			Iterator<Humor> ite1 = this.humors.keySet().iterator();
			while(ite1.hasNext()){
				tab[cpt++] = this.humors.get(ite1.next());
			}
			
			cbHumor = new JComboBox(tab);
		}
		
		
		return cbHumor;
	}

	private JPasswordField getTFPassword() {
		JPasswordField tfpassword =  new JPasswordField();
		if(this.toSee != null){
			tfpassword.setEditable(false);
		}
		return tfpassword;
	}

	private JButton getBtnOk() {
		JButton btnOk = new JButton();
		if(this.logout){
			btnOk.setText("Logout");
		}else if(this.toSee != null){
			btnOk.setText("Chat with");
			btnOk.setEnabled(this.chatWith);
		}else{
			btnOk.setText("Ok");
		}
		btnOk.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {	
				if(logout){
					appliManager.logout(toSee);
					frame.dispose();
				}else if(toSee == null){
					if(tfemail.getText().length() == 0 || tflogin.getText().length() == 0 || tfpassword.getPassword().length == 0){
						JOptionPane.showMessageDialog(frame, "Login, password or email are empty.", "Avatar error", JOptionPane.ERROR_MESSAGE);
					}else{
						boolean stop = false;
						Humor humor = Humor.COWS;
						Iterator<Humor> ite1 = humors.keySet().iterator();
						while(ite1.hasNext() && !stop){
							humor = ite1.next();
							if(humors.get(humor).equals(cbhumors.getSelectedItem())){
								stop = true;
							}
						}
						
						stop = false;
						Height height = Height.GEANT;
						Iterator<Height> ite2 = heights.keySet().iterator();
						while(ite2.hasNext() && !stop){
							height = ite2.next();
							if(heights.get(height).equals(cbheights.getSelectedItem())){
								stop = true;
							}
						}
						
						stop = false;
						Sex sex = Sex.MAN;
						Iterator<Sex> ite3 = sexs.keySet().iterator();
						while(ite3.hasNext() && !stop){
							sex = ite3.next();
							if(sexs.get(sex).equals(cbsexs.getSelectedItem())){
								stop = true;
							}
						}
						
						//System.out.println("name: "+tfname.getText()+", humor: "+humor+", height: "+height+", sex: "+sex+", email: "+tfemail.getText()+", password: "+tfname.getText());
						if(!appliManager.userConnected()){
							appliManager.createAvatar(tflogin.getText(), humor, height, sex, tfemail.getText(), new String(tfpassword.getPassword()));
						}else{
							appliManager.updateAvatar(height, humor, sex, tflogin.getText(), new String(tfpassword.getPassword()), tfemail.getText());
						}
										
						frame.dispose();
					}
				}else{
					
					new DialogWindow(appliManager, toSee);
					frame.dispose();
				
				}
				
				
			}
		});
		
		return btnOk;
	}

	private JTextField getTextFieldLogin() {
		JTextField tflogin = new JTextField();
		
		if(this.toSee != null){
			tflogin.setText(this.toSee.name);
			tflogin.setEditable(false);
		}else{
			if(this.appliManager.userConnected()){
				tflogin.setText(this.appliManager.getName());
				tflogin.setEditable(false);
			}
		}
		
		return tflogin;
	}

}
